package org.jinyan.db.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.jinyan.db.dao.IDBDao;
import org.jinyan.db.dao.ISuperMapDao;
import org.jinyan.db.utils.MysqlBuilder;

public class SuperMapDao implements ISuperMapDao {

	private IDBDao dbDao = new DBDao();

	@Override
	public boolean add(String tableName, Map<String, String> map)
			throws SQLException {
		if (!map.isEmpty()) {
			String sql = MysqlBuilder.buildAddSql(tableName, map);
			return dbDao.add(sql);
		} else {
			return false;
		}
	}

	@Override
	public boolean delete(String tableName, Map<String, String> map)
			throws SQLException {
		if (!map.isEmpty()) {
			String sql = MysqlBuilder.buildDeleteSql(tableName, map);
			return dbDao.delete(sql);
		} else {
			return false;
		}
	}

	@Override
	public boolean update(String tableName, Map<String, String> map,
			String pkAndValue) throws SQLException {
		if (!map.isEmpty()) {
			String sql = MysqlBuilder
					.buildUpdateSql(tableName, map, pkAndValue);
			return dbDao.update(sql);
		} else {
			return false;
		}
	}

	@Override
	public Object select(String tableName, Map<String, String> map)
			throws SQLException {
		if (!map.isEmpty()) {
			String sql = MysqlBuilder.buildSelectSql(tableName, map);
			List<Object> list = dbDao.selectList(sql);
			if (!list.isEmpty()) {
				return list.get(0);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public List<Object> listObject(String tableName, Map<String, String> map)
			throws SQLException {
		if (!map.isEmpty()) {
			String sql = MysqlBuilder.buildSelectSql(tableName, map);
			return dbDao.selectList(sql);
		} else {
			return null;
		}
	}

	@Override
	public boolean add(String sql) throws SQLException {

	   return dbDao.add(sql);
	}

	@Override
	public boolean delete(String sql) throws SQLException {
		return dbDao.delete(sql);
	}

	@Override
	public boolean update(String sql) throws SQLException {
		return dbDao.update(sql);
	}

	@Override
	public Object select(String sql) throws SQLException {
		
		return dbDao.select(sql);
	}

	@Override
	public List<Object> listObject(String sql) throws SQLException {
		return dbDao.selectList(sql);
	}

}
